import { createRouter,createWebHistory } from "vue-router";
import {checkUser,clearSession} from '../utils/authorization';
import { message} from 'ant-design-vue';
import index from '@/components/index/IndexHello.vue';
import login from '@/components/index/LoginPage.vue';
import loginNew from '@/components/index/LoginPageNew.vue';
import test1Hello from '../components/indexChildPage/Test1Hello';
import test2Hello from '../components/indexChildPage/Test2Hello';
import gatewayRouteConfig from '@/components/indexChildPage/GatewayRouteConfig';
import gatewayAllowPathConfig from '@/components/indexChildPage/GatewayAllowPathConfig';
import redirectPage from '@/components/index/RedirectPage';
import globalDictConfig from '@/components/indexChildPage/GlobalDictConfig';
import addMenu from '@/components/indexChildPage/AddMenu'
import uploadFile from '@/components/indexChildPage/UploadFile'

//路由数组
const routes = [
    { path:'/',component:uploadFile,name:'uploadFile',children:[]},
    { path:'/login',component:loginNew,name:'loginNew',children:[]},
    { path:'/index',component:index,name:'index',
        redirect: '/redirectPage',       
        children:[
            { path:'/redirectPage',component:redirectPage,name:'redirectPage'},
            { path:'/test1Hello',component:test1Hello,name:'test1Hello',children:[]},
            { path:'/test2Hello',component:test2Hello,name:'test2Hello',children:[]},
            { path:'/gatewayRouteConfig',component:gatewayRouteConfig,name:'GatewayRouteConfig',children:[]},
            { path:'/gatewayAllowPathConfig',component:gatewayAllowPathConfig,name:'gatewayAllowPathConfig',children:[]},
            { path:'/getGlobalDictConfig',component:globalDictConfig,name:'globalDictConfig',children:[]},
            { path:'/addMenu',component:addMenu,name:'addMenu',children:[]},
        ]
    }
]

const router = createRouter({
    history:createWebHistory(process.env.BASE_URL),
    routes
});

/**
 * 路由守卫
 * 登录和退出
 */
 router.beforeEach((to,from,next) => {
    let cookie=document.cookie;
    if (to.path === '/'){
        clearSession();
        next();
    } else {
        if (!checkUser()){
            router.push("/");
            message.warning("账号未登录,请登录!");
        }
        if(to.path.includes('/userServeController/logout')){
            clearSession();
            next();
        }
        next();
    }
});

export default router;